package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Cart;
import com.cskaoyan.bean.vo.CartCheckoutVo;
import com.cskaoyan.bean.wx.vo.CartVo;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/cart")
public class CartController {
    @Autowired
    CartService cartService;


    @GetMapping("goodscount")
    public BaseRespVo goodscount(){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        Integer count = cartService.goodscount(userId);
        return BaseRespVo.ok(count);
    }


    @GetMapping("index")
    public BaseRespVo index() {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        CartVo cartVo = cartService.index(userId);
        return BaseRespVo.ok(cartVo);
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();

        List<Integer> productIds = (List<Integer>) map.get("productIds");
        Boolean isChecked = (Integer) map.get("isChecked") == 0 ? false : true;

        CartVo cartVo = cartService.checked(productIds, isChecked,userId);
        return BaseRespVo.ok(cartVo);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody Cart cart) {
        cartService.update(cart);
        return BaseRespVo.ok();
    }


    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody Cart cart){
        Integer cartId = cartService.fastAdd(cart);
        return BaseRespVo.ok(cartId);
    }

    @PostMapping("add")
    public BaseRespVo add(@RequestBody Cart cart) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        cart.setUserId(userId);
        Integer acount = cartService.add(cart,userId);
        return BaseRespVo.ok(acount);
    }

    @GetMapping("checkout")
    public BaseRespVo checkout(Integer cartId, Integer addressId,
                               Integer couponId, Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        CartCheckoutVo cartCheckoutVo = cartService.checkout(cartId,addressId,couponId,grouponRulesId,userId);
        return BaseRespVo.ok(cartCheckoutVo);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        CartVo cartVo = cartService.delete(productIds, userId);
        return BaseRespVo.ok(cartVo);
    }
}
